import {Injectable} from "@angular/core";
import * as moment from "moment";


/**
 * 日期服务类
 */
@Injectable()
export class DateService {
    constructor() {

    }

    nowStamp: number = new Date().getTime();

    /**
     *  将时间戳转换成 今天 YYYY/MM/DD
     * @param {number} castDate
     * @returns {string}
     */
    public formatCastDate(castDate: number) {
        let date = formatDateTime(castDate);
        let now = moment(this.nowStamp).format('YYYY-MM-DD HH:MM');
        /* let date1=moment(castDate*1000).format('YYYY-MM-DD HH:MM');*/
        let result = '';


        /**
         * 格式化时间戳
         * @param timeStamp 时间戳
         * @param formatFlag 格式化连接字符 默认 -
         * @returns {string} 今天和明天，则返回 今天/明天 19:20,以前的则是 2018/10/29
         */
        function formatDateTime(timeStamp, formatFlag?) {
            formatFlag = formatFlag || '-';
            var date = new Date();
            if (/^\d{10}$/.test(timeStamp)) {
                timeStamp *= 1000;
            } else if (/^\d{13}$/.test(timeStamp)) {
                timeStamp = parseInt(timeStamp);
            } else {
                //'时间戳格式不正确！'
                return;
            }
            date.setTime(timeStamp);
            let y = date.getFullYear(),
                m = date.getMonth() + 1,
                month = m < 10 ? ('0' + m) : m,
                d = date.getDate(),
                day = d < 10 ? ('0' + d) : d,
                h = date.getHours(),
                hour = h < 10 ? ('0' + h) : h,
                miu = date.getMinutes(),
                minute = miu < 10 ? ('0' + miu) : miu;
            return y + formatFlag + month + formatFlag + day + ' ' + hour + ':' + minute;
        };

        let resultStamp = Math.floor((this.nowStamp - castDate * 1000) / (3600 * 24 * 1000));
        if (date.split(' ')[0] == now.split(' ')[0]) {
            result = '今天' + date.split(' ')[1];
        } else {
            if (resultStamp == -1) {
                result = '明天' + date.split(' ')[1];
            } else {
                result = formatDateTime(castDate, '/').split(' ')[0];
            }
        }

        return result;
    }

    /**
     * 向datatpicker添加年度顺序排列数据：[2010,2011,2012...2100]
     */
    getYearList(start: number, end: number = 2100) {
        let years = [];
        if (start < 1996) {
            start = 1996
        }
        if (typeof start == 'number' && typeof end == 'number' && start && end) {
            for (let y = start; y <= end; y++) {
                years.push(y)
            }
        } else {
            for (let y = 2010; y <= 2100; y++) {
                years.push(y)
            }
        }
        return years
    }

    selfFormate(date) {
        let y = date.getFullYear(),
            m = date.getMonth() + 1,
            month = m < 10 ? ('0' + m) : m,
            d = date.getDate(),
            day = d < 10 ? ('0' + d) : d,
            h = date.getHours(),
            hour = h < 10 ? ('0' + h) : h,
            miu = date.getMinutes(),
            minute = miu < 10 ? ('0' + miu) : miu;
        return y + '-' + month + '-' + day;
    }

    /**
     * 获取当月的第一天
     * @returns {Date}
     */
    getCurrentMonthFirstDay() {
        let date = new Date();
        date.setDate(1);
        return this.selfFormate(date);
    }

    getCurrentMonthLastDay() {
        let date = new Date(),
         currentMonth = date.getMonth(),
         nextMonth = ++currentMonth,
         nextMonthFirstDay =new Date(date.getFullYear(), nextMonth, 1),
         oneDay = 1000 * 60 * 60 * 24,
         thisDay=nextMonthFirstDay.getTime()- oneDay;
        return  this.selfFormate(new Date(thisDay));
    }
}
